﻿using HandyControl.Tools;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace BaseUi
{
    /// <summary>
    /// 权限转换器（转为bool）
    /// </summary>
    public class AuthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (DesignerHelper.IsInDesignMode)
                return true;

            return AuthConverter.IsAuth(parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 是否有权限
        /// </summary>
        /// <param name="value">认证项</param>
        /// <returns></returns>
        public static bool IsAuth(object value)
        {
            if (UserInfoView.viewModel.User == null || UserInfoView.viewModel.Roles == null || !UserInfoView.viewModel.Roles.Any())
                return false;
            if (UserInfoView.viewModel.User.IsAdmin || UserInfoView.viewModel.Roles.Any(o => o.IsAdmin))
                return true;
            if (value == null)
                return false;

            if (UserInfoView.viewModel.Roles.Any(o => o.Auths.Contains((int)value)))
                return true;
            else
                return false;
        }
    }
}
